package com.wlstock.funi.dao;

import java.sql.SQLException;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.wlstock.funi.model.AIAccountsModel;

public class AIAccountDao {
	private DataBaseHelper dbHelper;
	private static AIAccountDao daoInstance;
	private Dao<AIAccountsModel, Integer> aiAccountDao;
	
	@SuppressWarnings("unchecked")
	private AIAccountDao(Context mContext){
		try {
			dbHelper = DataBaseHelper.getHelper(mContext);
			aiAccountDao = dbHelper.getDao(AIAccountsModel.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static AIAccountDao getInstance(Context mContext){
		if (daoInstance == null) {
			synchronized (AIAccountDao.class) {
				if (daoInstance == null) {
					daoInstance = new AIAccountDao(mContext);
				}
			}
		}
		return daoInstance;
	}

	/**
	 * 
	 * @param aiModel
	 */
	public void addModel(AIAccountsModel aiModel) {
		try {
			aiAccountDao.createOrUpdate(aiModel);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public AIAccountsModel queryStock(String stockNo) {
		// TODO Auto-generated method stub
		try {
			AIAccountsModel tmpResult = aiAccountDao.queryBuilder().where().eq("stkCode", stockNo).queryForFirst();
			return tmpResult;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void delete(AIAccountsModel ownStock) {
		try {
			aiAccountDao.updateRaw("delete from AIAccountsModel where stkCode = ?", new String[]{ownStock.getStkCode()});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateHighestProRate(String stkCode, float hProfiteRate) {
		try {
			aiAccountDao.updateRaw("update AIAccountsModel SET hProfiteRate = ? where stkCode = ? ", new String[]{String.valueOf(hProfiteRate), stkCode});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 更新最高收益率以及当前收益率以及当前价格
	 * @param stkCode
	 * @param ownStock
	 */
	public void update(AIAccountsModel ownStock) {
		try {
			aiAccountDao.updateRaw("update AIAccountsModel SET hProfiteRate = ? , profitRate = ? , curPrice = ? where stkCode = ?", new String[]{String.valueOf(ownStock.gethProfiteRate()), String.valueOf(ownStock.getProfitRate()),String.valueOf(ownStock.getCurPrice()),ownStock.getStkCode()});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	
	
	
}
